package com.zzyl.controller.customer;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.ReservationDto;
import com.zzyl.service.ReservationService;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "客户预约管理")
@Slf4j
@RequiredArgsConstructor
public class CustomerReservationController extends BaseController {


    private final ReservationService reservationService;


    @GetMapping
    @ApiOperation("查询所有预约")
    public ResponseResult<List<ReservationVo>> findAll(@RequestParam(required = false) String mobile, @RequestParam(required = false) Long time) {
        List<ReservationVo> reservationVoList = reservationService.findAll(mobile, LocalDateTimeUtil.of(time));
        return success(reservationVoList);
    }


    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public ResponseResult cancelledCount(){
        log.info("查询取消预约数量");
        Integer numberTimes = reservationService.cancelledCount();
        return success(numberTimes);
    }

    @PostMapping
    @ApiOperation("新增预约")
    public ResponseResult addReservation(@RequestBody ReservationDto dto){
        log.info("新增预约:{}",dto);
        reservationService.addReservation(dto);
        return success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public ResponseResult<PageResponse<ReservationVo>> findPage(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "status",required = false, defaultValue = "0") Integer status
    ){
        log.info("分页查询{},{},{}",pageNum,pageSize,status);
        PageResponse<ReservationVo>  page = reservationService.findPage(pageNum,pageSize,status);
        return ResponseResult.success(page);
    }


    @GetMapping("countByTime")
    @ApiOperation("统计每个时间段的预约次数")
    public ResponseResult<List<TimeCountVo>> countByTime(Long time){
        log.info("统计每个时间段的预约次数,time：{}",time);
        List<TimeCountVo> timeCountVos = reservationService.countByTime(time);
        return success(timeCountVos);
    }

    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public ResponseResult cancelReservation(@PathVariable Long id){
        log.info("取消预约：{}",id);
        reservationService.cancelReservation(id);
        return success();
    }

}
